/*
 * 创建日期 2006-10-7
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.view;

import java.math.BigDecimal;
import org.apache.commons.validator.GenericValidator;
import org.apache.poi.hssf.usermodel.HSSFCell;

import com.vanda.report.ExcelUtils;
import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.element.Element;

/**
 * @author wangying
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public abstract class ArithmeticElement extends Element{
	
	private String cell_range;
	
	/**
	 * 标签执行方法，递归执行
	 * temp1:保存公式执行结果
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException{
		ReportInnerData reportData = (ReportInnerData)obj;
		try{
			//清空temp1
			BigDecimal result = (BigDecimal)reportData.getTemp1();
			
			String[] cells = cell_range.split(",");
			BigDecimal[] cellValues = new BigDecimal[cells.length];
			
			for(int i=0;i<cells.length;i++){
				HSSFCell cell = ExcelUtils.getCell(reportData.getReportSheet(),cells[i]);
				String strValue = ExcelUtils.getStringCellValue(cell);
				if(GenericValidator.isBlankOrNull(strValue)){
					strValue = "0";
				}
				cellValues[i] = new BigDecimal(strValue);
			}
			
			//公式计算
			result = aggregate(result,cellValues);

			reportData.setTemp1(result);
			
			super.execute(obj);
		}catch(ReportException e){
			throw e;
		}catch(Exception e){
			throw new ReportException(e);
		}
	}
	
	/**
	 * 聚集计算
	 * @param beanList
	 * @return
	 */
	public abstract BigDecimal aggregate(BigDecimal result,BigDecimal[] cellValues);

	/**
	 * @return
	 */
	public String getCell_range() {
		return cell_range;
	}

	/**
	 * @param string
	 */
	public void setCell_range(String string) {
		cell_range = string;
	}

}
